package sve.generator;

import java.util.ArrayList;

import sve.Utils;

import xadd.XADD;
import xadd.XADD.BoolDec;
import cmdp.HierarchicalParser;
import cmdp.TokenStream;

public class TrackingGen {

	public static void generate(String filePath, int count) {
		StringBuilder s = new StringBuilder("bvariables ( ");

		for (int i = 1; i <= count; i++) {
			s.append(" b" + i);
		}

		s.append(" ) \n	case test\n");

		String str = "";
		XADD x = new XADD();
		x.getVarIndex(x.new BoolDec("b1"), true);
		x.getVarIndex(x.new BoolDec("b2"), true);

		TokenStream t = new TokenStream();
		ArrayList<Integer> ids = new ArrayList<Integer>();
		ArrayList a;
		int id;

		for (int i = 1; i <= count; i++) {
			if (i == 1) {
				t.read("( b1  ( [0.6] )  ( [0.4] ) )");
				// t.read(VOIGen.getBlock("d > -10", VOIGen.getBlock("d < 10", "-0.01 * d * d + 0.001 * d + 1", "0"),
				// "0"));
				a = HierarchicalParser.ParseFileInt(t, 0);
				id = x.buildCanonicalXADD((ArrayList) a.get(0));
				s.append("b1 " + x.getString(id, false).replace("[b1]", "b1") + "\n");
				ids.add(id);

				t.read("( [x1 > 0.0] ( [x1 < 10] ( [0.1] ) ( [0.0] ) ) ( [0.0] ) )");
				a = HierarchicalParser.ParseFileInt(t, 0);
				id = x.buildCanonicalXADD((ArrayList) a.get(0));
				s.append("x1 " + x.getString(id, false) + "\n");
				ids.add(id);

				t.read("( b1 "
						+ VOIGen.getBlock("o1 > x1", VOIGen.getBlock("o1 < x1 + 2.0", "( [0.04 * (o1 - x1) + 0.28] )",
								" ( [-0.04 * (o1 - x1) + 0.28] )"), "0")
						+ " "
						+ VOIGen.getBlock("o1 > x1", VOIGen.getBlock("o1 < x1 + 2.0",
								"( [-0.001 * (o1 * o1 + x1 * x1 - 2.0 * o1 * x1 -  o1 - x1 ) + 0.13])",
								"( [-0.002 * (o1 * o1 + x1 * x1 - 2.0 * o1 * x1 -  o1 - x1 ) + 0.15] )"), "0") + " )");
				a = HierarchicalParser.ParseFileInt(t, 0);
				id = x.buildCanonicalXADD((ArrayList) a.get(0));
				s.append("o1 " + x.getString(id, false).replace("[b1]", "b1") + "\n");
				ids.add(id);
			} else {
				t.read("( b2  ( b1 ([1.0]) ([0.2]) )  ( b1 ( [0.0] ) ([0.8]) ) )");
				a = HierarchicalParser.ParseFileInt(t, 0);
				id = x.buildCanonicalXADD((ArrayList) a.get(0));
				str = "b" + i + " " + x.getString(id, false).replace("[b2]", "b" + i).replace("[b1]", "b" + (i - 1))
						+ "\n";
				// if (i > 2) {
				// str = str.replace("[b2]", "b" + i);
				// str = str.replace("[b1]", "b" + (i - 1));
				// }
				s.append(str);
				ids.add(id);

				t.read(VOIGen.getBlock("x2 > 0", VOIGen.getBlock("x2 < 10",
						VOIGen.getBlock("x1 > 0", VOIGen.getBlock("x1 < 10",
								"-0.024 * x2 * x2 - 0.024 * x1 * x1 + 2.0 * 0.024* x1 * x2 + 0.001 * x2 - 0.001 * x1 + 0.9",
								"0"), "0"), "0"), "0"));
				a = HierarchicalParser.ParseFileInt(t, 0);
				id = x.buildCanonicalXADD((ArrayList) a.get(0));
				str = "x2 " + x.getString(id, false) + "\n";
				if (i > 2) {
					str = str.replace("x2", "x" + i);
					str = str.replace("x1", "x" + (i - 1));
				}
				s.append(str);
				ids.add(id);

				t.read("( b1 "
						+ VOIGen.getBlock("o1 > x1", VOIGen.getBlock("o1 < x1 + 1.0", "( [0.04 * (o1 - x1) + 0.28] )",
								" ( [-0.04 * (o1 - x1) + 0.28] )"), "0")
						+ " "
						+ VOIGen.getBlock("o1 > x1", VOIGen.getBlock("o1 < x1 + 1.0",
								"( [-0.001 * (o1 * o1 + x1 * x1 - 2.0 * o1 * x1 -  o1 - x1 ) + 0.13])",
								"( [-0.002 * (o1 * o1 + x1 * x1 - 2.0 * o1 * x1 -  o1 - x1 ) + 0.15] )"), "0") + " )");
				a = HierarchicalParser.ParseFileInt(t, 0);
				id = x.buildCanonicalXADD((ArrayList) a.get(0));
				str = "o1 " + x.getString(id, false) + "\n";
				s.append(str.replace("o1", "o" + i).replace("[b1]", "b" + i).replace("x1", "x" + i));
				ids.add(id);
			}
		}

		Utils.print(filePath + ".sve", s.append("\nendcase").toString());
	}
}
